//
//  SDLeftViewController.m
//  SuperDoctor
//
//  Created by 李忠 on 7/5/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDLeftViewController.h"
#import "SDLoginViewController.h"
#import "SDRegisterViewController.h"
#import "SDDoctorApplyController.h"
#import "SDCaseManageController.h"
#import "SDFocusDoctorController.h"
#import "SDSettingViewController.h"
#import "SDAboutProducController.h"
#import "SDSharedViewController.h"
#import "NSUserDefaults+SDExtension.h"
#import "SDShowPersonalInfo.h"

@interface SDLeftViewController ()<UITableViewDelegate, UITableViewDataSource>

//没有登陆界面
@property (nonatomic, strong) UIView *noLoginBGView;

//有登陆界面
@property (nonatomic, strong) UIView *loginBGView;

//TableView
@property (nonatomic, strong) UITableView *tableView;

//tableView的两个数据源
@property (nonatomic, strong) NSArray *arrData1;
@property (nonatomic, strong) NSArray *arrData2;

@property (nonatomic, weak) UIView *showView;
@end

static NSString *const tableId = @"cell";

@implementation SDLeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupConfig];
    [self setupLayout];
}

- (void)setupConfig {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.arrData1 = @[@"名医通申请",@"病历管理",@"关注医生"];
    self.arrData2 = @[@"设置与帮助",@"关于帮助",@"分享好友"];
        
}

- (void)setupLayout{
    if (_showView) {
        _showView.hidden = YES;
    }
    _showView = [self checkLoginStatus] ? self.loginBGView : self.noLoginBGView;
    _showView.hidden = NO;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.top.mas_equalTo(_showView.mas_bottom);
        make.leading.bottom.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_LEFT_WIDTH);
        
    }];
    [self.tableView reloadData];
}

- (BOOL)checkLoginStatus{
    
    BOOL status = [[NSUserDefaults standardUserDefaults] getBoolForkey:kAccountStatus];
    return status;
}

#pragma mark
#pragma mark - tableView的数据源代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableId];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableId];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    if (indexPath.section == 0) {
        
            cell.textLabel.text = self.arrData1[indexPath.row];
    }else{
        
        cell.textLabel.text = self.arrData2[indexPath.row];
        
    }
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    UIViewController *vc = nil;
    if (section == 0) {
        
        switch (row) {
            case 0:
                vc = [[SDDoctorApplyController alloc] init];
                break;
            case 1:
                vc = [[SDCaseManageController alloc] init];
                break;
            case 2:
                vc = [[SDFocusDoctorController alloc] init];
                break;
                
            default:
                break;
        }
        vc.navigationItem.title = self.arrData1[row];
    }else if (section == 1) {
        switch (row) {
            case 0:
                vc = [[SDSettingViewController alloc] init];
                break;
            case 1:
                vc = [[SDAboutProducController alloc] init];
                break;
            case 2:
                vc = [[SDSharedViewController alloc] init];
                break;
                
            default:
                break;
        }
        vc.navigationItem.title = self.arrData2[row];
    }
    
    [self jumpOtherViewController:vc];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    return 5.0;
}

- (void)clickButton:(UIButton *)sender{
    //点击登陆
    if (sender.tag == 101) {
        SDLoginViewController *login = [[SDLoginViewController alloc] init];
        login.title = @"登陆";
        [self jumpOtherViewController:login];
        
    }
    //点击注册
    if (sender.tag == 102) {
        SDRegisterViewController *regist = [[SDRegisterViewController alloc] init];
        regist.title = @"注册";
        [self jumpOtherViewController:regist];
    }
}

#pragma mark
#pragma mark - 懒加载

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}


- (UIView *)noLoginBGView{
    if (!_noLoginBGView) {
        
        _noLoginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        _noLoginBGView.backgroundColor = kThemeColor;
        [self.view addSubview:_noLoginBGView];
        //MARK: 头像
        UIImageView *iconV = [[UIImageView alloc] init];
        iconV.image = [UIImage imageNamed:@"name"];
        iconV.bounds = CGRectMake(0, 0, 45, 45);
        iconV.center = CGPointMake(SCREEN_LEFT_WIDTH * 0.5, 50);
        [_noLoginBGView addSubview:iconV];
        
        //MARK: 登陆
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_noLoginBGView addSubview:leftButton];
        leftButton.tag = 101;
        [leftButton setTitle:@"登陆" forState:UIControlStateNormal];
        [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [leftButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).mas_offset(10);
            make.trailing.mas_equalTo(iconV.mas_centerX).mas_offset(-10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
            
        }];
        
        //MARK: 注册
        UIButton *rigthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_noLoginBGView addSubview:rigthButton];
        rigthButton.tag = 102;
        [rigthButton setTitle:@"注册" forState:UIControlStateNormal];
        
        [rigthButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rigthButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [rigthButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV.mas_bottom).mas_offset(10);
            make.leading.mas_equalTo(iconV.mas_centerX).mas_offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 30));
            
        }];
    }
    return _noLoginBGView;
}

- (UIView *)loginBGView{
    if (!_loginBGView) {
        
        _loginBGView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
        _loginBGView.backgroundColor = kThemeColor;
        [self.view addSubview:_loginBGView];
        
        //MARK: 头像
	    UIImageView *iconV = [[UIImageView alloc] init];
	    iconV.image = [UIImage imageNamed:@"content_img_01"];
	    iconV.layer.cornerRadius = 35;
	    iconV.layer.masksToBounds = YES;
	    iconV.userInteractionEnabled = YES;
	    iconV.multipleTouchEnabled = YES;
	    
        [_loginBGView addSubview:iconV];
        [iconV mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_equalTo(20);
            make.top.mas_equalTo(30);
            make.size.mas_equalTo(CGSizeMake(70, 70));
            
        }];
        
        //MARK: 昵称
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = @"王二狗";
        nameLabel.font = FONT(16);
        [_loginBGView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(iconV).mas_offset(5);
            make.left.mas_equalTo(iconV.mas_right).mas_offset(15);
            
        }];
        
        //MARK: 详细信息
	    UILabel *descLabel = [[UILabel alloc] init];
	    descLabel.text = @"查看个人信息";
	    descLabel.font = FONT(16);
	    
        [_loginBGView addSubview:descLabel];
	    
	    descLabel.userInteractionEnabled = YES;
	    descLabel.multipleTouchEnabled = YES;
        
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(iconV).mas_offset(-5);
            make.left.mas_equalTo(iconV.mas_right).mas_equalTo(15);
        }];
	    
	    
	    UITapGestureRecognizer * labelTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
	    
	    UITapGestureRecognizer * iconTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
		
	    
	    [descLabel addGestureRecognizer:labelTap];
	    
	    [iconV addGestureRecognizer:iconTap];
    }
    return _loginBGView;
}

- (void)tap:(UITapGestureRecognizer *)tap{
	
		SDShowPersonalInfo * showInfoVC = [[SDShowPersonalInfo alloc]init];
		
		[self presentViewController:showInfoVC animated:YES completion:nil];
		
}

@end
